package com.xmb.common.interceptor;

import com.xmb.common.entity.structure.CurrentUser;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class UserInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setCharacterEncoding("UTF-8");
        //从header中获取到用户的信息
        String openid = request.getHeader("openid");
        Long userId = Long.valueOf(request.getHeader("userId"));
        String appid = request.getHeader("appid");
        String supplierId = request.getHeader("supplierId");

//        String openid  = "oS6kN5Gtz8lJc8n6PdCv-TRNY0lw";
//        Long userId = 568865325471236096L;
//        String appid = "wx423e2c8e95f14239";
//        String supplierId = "xmb34ws3l6097343274";
        CurrentUser currentUser = new CurrentUser(openid,userId,appid,supplierId);
        CurrentContextUser.set(currentUser);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
